package com.example.yijian.edit.view.popup;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.yijian.R;
import com.example.yijian.edit.view.EditChangeSpeedScrollView;
import com.example.yijian.utils.datainfo.ClipInfo;

import java.util.ArrayList;
import java.util.List;

public class SpeedNormalPopup extends BaseCommonPopupWindow {
    private ImageView mIvConfirm;
    private LinearLayout mLlSpeedContainerView;
    private CheckBox mCheckBox;

    private EditChangeSpeedScrollView speedScrollView;
    private List<EditChangeSpeedScrollView.SpeedParam> speedParams;
    private float mSpeed;
    private boolean keepAudioPitch  = true;
    private OnFunctionListener onFunctionListener;

    public SpeedNormalPopup(Context context) {
        super(context);
        setContentView(R.layout.popup_speed_normal);
        initView();
        initData();
        initListener();
    }

    protected void initView() {
        mLlSpeedContainerView = getContentView().findViewById(R.id.ll_speed_container);
        mIvConfirm = getContentView().findViewById(R.id.speedFinish);
        mCheckBox = getContentView().findViewById(R.id.ck_change_voice);
        speedScrollView = getContentView().findViewById(R.id.speed_view);
    }

    protected void initData() {
        speedParams = new ArrayList<>();
        EditChangeSpeedScrollView.SpeedParam param0 = new EditChangeSpeedScrollView.SpeedParam(0.2f);
        EditChangeSpeedScrollView.SpeedParam param1 = new EditChangeSpeedScrollView.SpeedParam(1f);
        EditChangeSpeedScrollView.SpeedParam param2 = new EditChangeSpeedScrollView.SpeedParam(2f);
        EditChangeSpeedScrollView.SpeedParam param3 = new EditChangeSpeedScrollView.SpeedParam(3f);
        EditChangeSpeedScrollView.SpeedParam param4 = new EditChangeSpeedScrollView.SpeedParam(4f);
        speedParams.add(param0);
        speedParams.add(param1);
        speedParams.add(param2);
        speedParams.add(param3);
        speedParams.add(param4);
        speedScrollView.postDelayed(() -> speedScrollView.setSelectedData(speedParams), 200);
    }

    public void setData(ClipInfo clipInfo) {
        this.mSpeed = clipInfo.getSpeed();
        this.keepAudioPitch = clipInfo.isKeepAudioPitch();
        mCheckBox.setChecked(keepAudioPitch);
        speedScrollView.setCurrentSpeed(mSpeed);
    }

    public void setSpeed(float speed, boolean isChangeVoice) {
        mSpeed = speed;
        mCheckBox.setChecked(isChangeVoice);

        speedScrollView.setCurrentSpeed(speed);
    }

    protected void initListener() {
        mIvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (null != onFunctionListener) {
                    onFunctionListener.onSpeedChanged(mSpeed, mCheckBox.isChecked());
                }
            }
        });
        /**
         * 变调
         */
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (null != onFunctionListener) {
                    onFunctionListener.onSpeedChanged(mSpeed, isChecked);
                }
            }
        });

        mLlSpeedContainerView.setOnTouchListener((v, event) -> true);

        speedScrollView.setOnSpeedChangedListener(new EditChangeSpeedScrollView.OnSpeedChangedListener() {
            @Override
            public void onSpeedChanged(float speed) {
                mSpeed = speed;
                if (null != onFunctionListener) {
                    onFunctionListener.onSpeedChanged(speed, mCheckBox.isChecked());
                }
            }
        });
    }

    public void setOnFunctionListener(OnFunctionListener callBack) {
        this.onFunctionListener = callBack;
    }


    public interface OnFunctionListener {
//        void onConfirm(float speed, boolean changeVoice);
//
//        void onChangeVoice(float speed, boolean changeVoice);

        void onSpeedChanged(float speed, boolean changeVoice);
    }

}
